//
//  QJKDVCThree.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/28.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJKDVCThree.h"
#import "QJKDVCFour.h"
@interface QJKDVCThree () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UIImage *yyImage;
@end

@implementation QJKDVCThree

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"开店";
    

}


- (IBAction)qyBtnClickAction:(UIButton *)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择图片来源" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gotoIamgePickerWith: UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gotoIamgePickerWith: UIImagePickerControllerSourceTypePhotoLibrary];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cameraAction];
    [alert addAction:photoAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark -- 选择图片来源
- (void)gotoIamgePickerWith:(NSUInteger)sourceType{
    if (sourceType == UIImagePickerControllerSourceTypeCamera) {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            [MBPHUD showAlertWithTitle:@"您没有对相机授权!"];
            return;
        }
        
    }else{
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [MBPHUD showAlertWithTitle:@"您没有对相册授权!"];
            return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:^{
    }];
}
#pragma mark UIImagePickerControllerDelegate
//该代理方法仅适用于只选取图片时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    //    NSData *data = UIImageJPEGRepresentation(image, 1);
    self.yyImage = image;
    [self.qyImageBtn setImage:image forState:UIControlStateNormal];
    
}
- (IBAction)nextBtnClickAction:(UIButton *)sender {  // 开店按钮
    
//    //* 测试
//
//    self.kdModel.companyLicenseCode = @"9999999999";
//    self.kdModel.companyLicenseUrl = @"shop/20180523/9cc1b6e69a65c563388e1db7caafdf07_thumb_500_500.jpg";
//
//    // 直接上传, 不跳转了
//    [self uploadKaiDianApplyAction];
//
//    //*/ 测试结束
    
    
    NSString *zzNum = [NSString stringWithFormat:@"%@",self.yyzzTF.text];
    if (zzNum.length > 0 && self.yyImage) {
        self.kdModel.companyLicenseCode = zzNum;
        [self uploadImageOneAction];
    }else{
        [MBPHUD showAlertWithTitle:@"请检查所需信息是否完整!"];
    }
}

#pragma mark -- 提交开店申请
- (void)uploadKaiDianApplyAction{
    
    NSLog(@"companyName = %@", self.kdModel.companyName);
    NSLog(@"linkName = %@", self.kdModel.linkName);
    NSLog(@"tel = %@", self.kdModel.tel);
    NSLog(@"provice = %@", self.kdModel.provice);
    NSLog(@"city = %@", self.kdModel.city);
    NSLog(@"region = %@", self.kdModel.region);
    NSLog(@"detailAddress = %@", self.kdModel.detailAddress);
    NSLog(@"legalPerson = %@", self.kdModel.legalPerson);
    NSLog(@"idCardUrl = %@", self.kdModel.idCardUrl);
    NSLog(@"idCardUrl2 = %@", self.kdModel.idCardUrl2);
    NSLog(@"longitude = %@", self.kdModel.longitude);
    NSLog(@"latitude = %@", self.kdModel.latitude);
    NSLog(@"companyLicenseCode = %@", self.kdModel.companyLicenseCode);
    NSLog(@"companyLicenseUrl = %@", self.kdModel.companyLicenseUrl);
    NSLog(@"userName = %@", self.kdModel.userName);
    NSLog(@"shopName = %@", self.kdModel.shopName);
    NSLog(@"categoryNearId = %@", self.kdModel.categoryNearId);
    NSLog(@"image_url = %@", self.kdModel.image_url);

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:self.kdModel.companyName forKey:@"companyName"];
    [dict setValue:self.kdModel.linkName forKey:@"linkName"];
    [dict setValue:self.kdModel.tel forKey:@"tel"];
    [dict setValue:self.kdModel.provice forKey:@"provice"];
    [dict setValue:self.kdModel.city forKey:@"city"];
    [dict setValue:self.kdModel.region forKey:@"region"];
    [dict setValue:self.kdModel.detailAddress forKey:@"detailAddress"];
    [dict setValue:self.kdModel.legalPerson forKey:@"legalPerson"];
    [dict setValue:self.kdModel.idCardUrl forKey:@"idCardUrl"];
    [dict setValue:self.kdModel.idCardUrl2 forKey:@"idCardUrl2"];
    [dict setValue:self.kdModel.companyLicenseCode forKey:@"companyLicenseCode"];
    [dict setValue:self.kdModel.companyLicenseUrl forKey:@"companyLicenseUrl"];
    [dict setValue:self.kdModel.userName forKey:@"userName"];
    [dict setValue:self.kdModel.shopName forKey:@"shopName"];
    [dict setValue:self.kdModel.categoryNearId forKey:@"categoryNearId"];
    [dict setValue:self.kdModel.longitude forKey:@"longitude"];
    [dict setValue:self.kdModel.latitude forKey:@"latitude"];
    [dict setValue:self.kdModel.image_url forKey:@"image_url"];
    
    NSLog(@"开店 dict = %@", dict);
    [WWNetRequest fetchOpenShopWithParameters:dict success:^(id response) {
        NSLog(@"开店 response = %@", response);
        [MBPHUD showAlertWithTitle:@"您的申请已提交成功,请等待系统审核!"];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSDictionary *error) {
        NSLog(@"开店 error = %@", error);
    }];
}

#pragma mark -- 图片上传
- (void)uploadImageOneAction{
    
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:nil withImageFile:self.yyImage hiddenHUD:YES HUDTitle:@"正在提交信息..." success:^(NSURLSessionDataTask *operetion, id response) {
        if (response) {
            self.kdModel.companyLicenseUrl = response[@"url"];
            
            [self uploadKaiDianApplyAction];
        }
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        self.kdModel.companyLicenseCode = @"";
        self.kdModel.companyLicenseUrl = @"";
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
